package service.impl;

import java.io.Serializable;
import java.util.List;

import dao.impl.GenericDaoImpl;

import service.GenericService;
import util.PageBean;

public class GenericServiceImpl<T,ID extends Serializable,GeneralDao extends GenericDaoImpl<T, ID>> implements GenericService<T, ID>{

	protected GeneralDao generalDao;
	
	
	
	public GeneralDao getGeneralDao() {
		return generalDao;
	}

	public void setGeneralDao(GeneralDao generalDao) {
		this.generalDao = generalDao;
	}

	@Override
	public ID save(T t) {
		// TODO Auto-generated method stub
		return generalDao.save(t);
	}

	@Override
	public List<T> queryAll() {
		// TODO Auto-generated method stub
		return generalDao.queryAll();
	}

	@Override
	public List<T> queryByHql(String queryString) {
		// TODO Auto-generated method stub
		return generalDao.queryByHql(queryString);
	}

	@Override
	public List<T> queryByNamedParam(String queryString, String[] paramNames,
			Object[] values) {
		// TODO Auto-generated method stub
		return generalDao.queryByNamedParam(queryString, paramNames, values);
	}

	@Override
	public T queryById(ID id) {
		// TODO Auto-generated method stub
		return generalDao.queryById(id);
	}

	@Override
	public void delete(ID id) {
		// TODO Auto-generated method stub
		generalDao.delete(id);
	}

	@Override
	public void delete(T t) {
		// TODO Auto-generated method stub
		generalDao.delete(t);
	}

	@Override
	public void update(T t) {
		// TODO Auto-generated method stub
		generalDao.update(t);
	}

	@Override
	public void saveOrUpdate(T t) {
		// TODO Auto-generated method stub
		generalDao.saveOrUpdate(t);
	}

	@Override
	public PageBean queryForPage(String queryString, int pageSize, int page) {
		// TODO Auto-generated method stub
		int allRow = generalDao.getAllRowCount(queryString);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		page = page>totalPage?totalPage:page;
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		
		List<T> list = generalDao.queryForPage(queryString, offset, length);
		
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		
		return pageBean;
	}

}
